/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag;

import java.util.Map;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTheme;

import org.springframework.stereotype.Component;

import com.jgoodies.looks.plastic.PlasticLookAndFeel;
import com.jgoodies.looks.plastic.PlasticTheme;

import sk.jp.commons.exception.ApplicationException;
import sk.jp.commons.utils.ExceptionUtils;
import sk.jp.commons.utils.StringUtils;
import sk.jp.jitag.model.config.Profile;
import sk.jp.jitag.util.Defaults;
import sk.jp.jitag.util.Property;

/**
 * Services for user interface management.
 */
@Component
public class UiManager {

    /**
     * Setup <i>look and feel</i> class and theme, specified in given <code>profile<code>.
     * @param profile configuration profile. Uses {@link Profile#getLafClass()} and
     * {@link Profile#getLafTheme()}.
     */
    public void setupLookAndFeel(Profile profile){
        try {
            String lafClass = profile.getLafClass();

            //L&F Theme initialization
            String lafTheme = profile.getLafTheme();
            if(!StringUtils.isNullOrEmpty(lafTheme)){
                Map<String,String> lafThemes = Defaults.lafThemes.get(lafClass);
                if(lafThemes != null && lafThemes.containsKey(lafTheme)){
                    //Resolve theme for current L&F
                    if(lafClass.equals(MetalLookAndFeel.class.getName())){
                        String themeClassName = lafThemes.get(lafTheme);
                        MetalTheme metalTheme = (MetalTheme) getClass().getClassLoader()
                            .loadClass(themeClassName).newInstance();
                        MetalLookAndFeel.setCurrentTheme(metalTheme);
                    }else if(lafClass.equals(Defaults.LafClass.synthetica.getClassName())){
                        lafClass = lafThemes.get(lafTheme);
                    }else if(lafClass.equals(Defaults.LafClass.jgoodiesPl.getClassName())){
                        String themeClassName = lafThemes.get(lafTheme);
                        PlasticTheme theme = (PlasticTheme) getClass().getClassLoader()
                            .loadClass(themeClassName).newInstance();
                        PlasticLookAndFeel.setPlasticTheme(theme);
                        PlasticLookAndFeel.setCurrentTheme(theme);
                    }
                }
            }

            //L&F setup
            if(StringUtils.isNullOrEmpty(lafClass)){
                lafClass = Property.defaultLookAndFeel.value();
            }
            UIManager.setLookAndFeel(lafClass);
        } catch (Exception e) {
            throw new ApplicationException("Can't setup UI look and feel",
                                           null,
                                           ExceptionUtils.paramsToMap("profile", profile),
                                           e);
        }
    }

}
